import datetime
import json
import os


def red_json(filePath='/VoiceInfo.json'):  # 保持原始参数定义
    # 添加路径拼接逻辑
    base_dir = os.path.abspath(os.path.dirname(__file__))
    file_path = os.path.join(base_dir, filePath.lstrip('/'))  # 修正路径拼接方式

    try:
        # 读取 JSON 文件
        data = None
        with open(file_path, 'r', encoding='utf-8') as f:
            data = json.load(f)  # 修正为使用文件对象f

        print("读取成功")
    except FileNotFoundError:
        print(f"未找到 JSON 文件: {file_path}")  # 添加具体路径显示
    except Exception as e:
        print(f"插入数据时出现错误: {e}")
    return data


def write_json(file_path='\VoiceInfo.json',data=None):

    file_path=os.path.join(os.path.abspath(os.path.dirname(__file__)), file_path)
    print("file_path",file_path)
    try:
        with open(file_path, "w", encoding="utf-8") as f:
            json.dump(data, f, indent=2, ensure_ascii=False)  # 修正参数为文件对象f
        print("数据写入成功")
        return True
    except FileNotFoundError:
        print(f"路径不存在: {file_path}")
    except PermissionError:
        print(f"无写入权限: {file_path}")
    except Exception as e:
        print(f"写入数据时出现错误: {e}")
    return False





def get_current_time():
    now = datetime.datetime.now()
    return now.strftime("%Y-%m-%d %H:%M:%S")

